#include <iostream>
#include <string>
#include <unistd.h>
#include <sw/redis++/redis++.h>
using namespace sw::redis;
#define redis_server_url "tcp://127.0.0.1:6379"

int main()
{
    Redis redis(redis_server_url);
    std::string result = redis.ping();
    std::cout << result << std::endl;
    redis.flushall();
    {
        printf("开始执行get、set命令了\n");
        if (!redis.set("user", "12345678"))
        {
            std::cerr << "set失败!" << std::endl;
            return -1;
        }
        OptionalString val1 = redis.get("user");
        if (val1.has_value())
        {
            std::cout << val1.value() << std::endl;
        }

        std::initializer_list<std::pair<std::string, std::string>> list = {{"pass", "347823"}, {"data", "学生"}};
        redis.mset(list);

        OptionalString val2 = redis.get("pass");
        if (val2.has_value())
        {
            std::cout << "pass : " << val2.value() << std::endl;
        }
        printf("--------------------------------\n");
    }

    {
        printf("开始执行exists命令了\n");
        long long ret = redis.exists({"user", "pass", "data"});
        std::cout << "exists command result: " << ret << std::endl;
        printf("--------------------------------\n");
    }
    {
        printf("开始执行del命令了\n");
        // 删除单个
        printf("删除单个key\n");
        long long ret = redis.del("pass");
        std::cout << "del command result: " << ret << std::endl;
        printf("删除多个key\n");
        ret = redis.del({"user", "data"});
        std::cout << "del command result: " << ret << std::endl;
        printf("--------------------------------\n");
    }

    {
        printf("开始执行keys了\n");
        redis.flushall();
        redis.set("k1", "111");
        redis.set("k2", "222");
        redis.set("k3", "333");
        redis.set("k4", "444");
        std::vector<std::string> rets;
        auto it = std::back_inserter(rets);
        redis.keys("*",it);

        for(auto x : rets) std::cout<<x<<std::endl;
         printf("--------------------------------\n");
    }

    {
        printf("开始执行expire了\n");
        redis.flushall();
        redis.set("k1", "111");
        redis.set("k2", "222");
        redis.set("k3", "333");
        redis.set("k4", "444");
        OptionalString ret = redis.get("k1");
        if(ret.has_value())
        {
            std::cout<<"val: "<<ret.value()<<std::endl;
        }
        //设置5秒后过期
        redis.expire("k1",5);
        //获取指定key剩余过期时间
        long long sec = redis.ttl("k1");
        while(sec--)
        {
            std::cout<<"k1剩余过期时间: "<<sec<<std::endl;
            sleep(1);
        }
        ret = redis.get("k1");
        if(ret.has_value())
        {
            std::cout<<"val: "<<ret.value()<<std::endl;
        }
        else printf("k1已过期!\n");
        printf("--------------------------------\n");
    }

    {
        printf("开始执行type命令了!\n");
        redis.flushall();
        redis.set("k1", "111");
        redis.set("k2", "222");
        redis.set("k3", "333");
        redis.set("k4", "444");
        std::string type = redis.type("k1");
        if(!type.empty()) std::cout<<"k1数据类型:"<<type<<std::endl;
        else std::cout<<"type为空"<<std::endl;
        printf("--------------------------------\n");
    }
    return 0;
}